//package com.jumi.microservice.mq;
//
//import cn.hutool.core.convert.Convert;
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.jumi.microservice.dto.wallet.api.WalletInOrOutRequest;
//import com.jumi.microservice.entity.JmUser;
//import com.jumi.microservice.entity.JmUserFenxiao;
//import com.jumi.microservice.enums.CommonEnum;
//import com.jumi.microservice.enums.FenXiaoStatus;
//import com.jumi.microservice.service.IErrOrderAfterMsgService;
//import com.jumi.microservice.service.IJmUserFenxiaoService;
//import com.jumi.microservice.service.IJmUserService;
//import com.jumi.microservice.service.IWalletInOrOutService;
//import com.jumi.microservice.util.WrapperUtil;
//import org.apache.dubbo.config.annotation.Reference;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.core.RocketMQListener;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.math.BigDecimal;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * 订单完成,确认收货接收mq 将受益转为可到账
// */
//@Component
//@RocketMQMessageListener(topic = "orderAccomplish",consumerGroup = "member-accomplish")
//public class OrderAccomplishMq implements RocketMQListener<String> {
//    @Autowired
//    IErrOrderAfterMsgService errOrderAfterMsgService;
//
//    @Autowired
//    IJmUserFenxiaoService fenxiaoService;
//
//    @Reference(check = false)
//    IWalletInOrOutService walletInOrOutService;
//
//    @Autowired
//    IJmUserService userService;
//
//    Logger logger= LoggerFactory.getLogger(OrderAccomplishMq.class);
//
//    @Override
//    @Transactional
//    public void onMessage(String message) {
//        Map map = JSONObject.parseObject(message, Map.class);
//        Object buyerId = map.get("buyerId");
//        Object orderNo = map.get("orderNo");
//        JmUserFenxiao fenxiao=new JmUserFenxiao();
//        fenxiao.setIsDel(CommonEnum.否); fenxiao.setBuyerId(Integer.valueOf(buyerId.toString()));
//        fenxiao.setOrderSn(orderNo.toString());
//        QueryWrapper wrapper = WrapperUtil.getWrapper(null, fenxiao);
//        List<JmUserFenxiao> list = fenxiaoService.list(wrapper);
//        List<WalletInOrOutRequest> walletInList=new ArrayList<>();//批量入账
//
//        BigDecimal bd=new BigDecimal(0.0);//用户的收益
//
//        if(ObjectUtil.isNotEmpty(list)){
//            for (JmUserFenxiao jmUserFenxiao : list) {
//                WalletInOrOutRequest request=new WalletInOrOutRequest();
//                BigDecimal yongjin = jmUserFenxiao.getYongjin();
//                request.setAmount(yongjin);
//                request.setOrderNo(jmUserFenxiao.getOrderSn());
//                request.setType(jmUserFenxiao.getType().getValue());
//                request.setUserId(jmUserFenxiao.getId().longValue());
//
//                jmUserFenxiao.setStatus(FenXiaoStatus.已结算);
//                jmUserFenxiao.setUpdateTime(LocalDateTime.now());
//                walletInList.add(request);
//                bd.add(yongjin);
//            }
//            JmUser jmUser = userService.getById(Convert.toInt(buyerId));
//            if(ObjectUtil.isNotEmpty(jmUser)){
//                //更新用户的可提现金额;
//                BigDecimal availablePredeposit = jmUser.getAvailablePredeposit();//用户可提现余额
//                availablePredeposit=availablePredeposit.add(bd);
//                jmUser.setAvailablePredeposit(availablePredeposit);
//                userService.updateById(jmUser);
//            }
//            //更新用户收益记录为已结算
//            fenxiaoService.updateBatchById(list);
//            try {
//                //调用钱包服务
//                walletInOrOutService.batchWalletIn(walletInList);
//            }catch (Exception e){
//                logger.error(StrUtil.format("wallet 服务调用错误{}",e.getMessage()));
//            }
//        }
//        logger.info("订单完成消息消费结束---->");
//    }
//}
